﻿CREATE OR REPLACE FUNCTION controldehoras() RETURNS trigger AS $controlhoras$
	BEGIN
		UPDATE carga SET horasalmacenada=extract(epoch from age(current_timestamp,fechaingreso))/3600; 
	RETURN NEW;
	END;
$controlhoras$ LANGUAGE plpgsql;

CREATE TRIGGER controlhoras BEFORE INSERT OR UPDATE OR DELETE ON carga 
FOR EACH STATEMENT EXECUTE PROCEDURE controldehoras();


CREATE OR REPLACE FUNCTION controldealmacenes() RETURNS trigger AS $controlespacio$
	BEGIN
		UPDATE carga SET espaciolibre=espaciolibre-1;
		IF espaciolibre=0 THEN UPDATE carga SET estado=true; 
		END IF; 
	RETURN NEW;
	END;
$controlespacio$ LANGUAGE plpgsql;

CREATE TRIGGER controlespacio AFTER INSERT ON carga

FOR EACH STATEMENT EXECUTE PROCEDURE controldealmacenes();